package com.bfxy.rabbitmq.api.dlx;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * 死信队列的消费 -- 消息补偿被消费掉了
 * create by zhangtian3 on 2019/3/28
 */
public class DXLConsumer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory() ;
        connectionFactory.setHost("106.12.109.239");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("123456");
        connectionFactory.setVirtualHost("/hadoop");

        Connection connection = connectionFactory.newConnection() ;
        Channel channel = connection.createChannel() ;

        String exchangeName = "dlx.exchange" ;
        String queueName = "dlx.queue" ;

        channel.exchangeDeclare(exchangeName, "topic", true, false, null);
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, "");

        channel.basicConsume(queueName, true, new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.err.println("-----------consume message----------");
                System.err.println("consumerTag: " + consumerTag);
                System.err.println("envelope: " + envelope);
                System.err.println("properties: " + properties);
                System.err.println("body: " + new String(body));
            }
        });
    }
}
